<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title></title>
</head>
<body>
<script type="text/javascript">
    //创建私有的对象常量，并为其提供修改和获取其值的方法
    var constant = (function(){
        var constants = {},         //存放对象私有常的对象
            ownProp = Object.prototype.hasOwnProperty,   //缓存
            allowed = {                    // 此对象是用来检测私有常量的值的类型
                string: 1,
                number: 1,
                boolean: 1
            },
            prefix = (Math.random() + "_").slice(2);
        return {
            set: function(name, value) {
                if (this.isDefined(name)) {
                    return false;
                }
                if (!ownProp.call(allowed, typeof value)) {
                    return false;
                }
                constants[prefix + name] = value;
                return true;
            },
            isDefined: function(name) {
                return ownProp.call(constants, prefix + name);
            },
            get: function(name) {
                if (this.isDefined(name)) {
                   return constants[prefix + name];
                }
                return null;
            }
        };
    }());
    constant.set("aa", 0);
    alert(constant.get("aa"));

</script>
</body>
</html>